//
//  ZYLFastLookTableViewCell.m
//  Cartoon
//
//  Created by dllo on 14/7/15.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "ZYLFastLookTableViewCell.h"
#import "UIImageView+WebCache.h"
#import "ReadViewController.h"
#define SCREEN_WIDTH ([[UIScreen mainScreen] bounds].size.width)
#define SCREEN_HEIGHT ([[UIScreen mainScreen] bounds].size.height)

@implementation ZYLFastLookTableViewCell
- (void)dealloc
{
    [_author release];
    [_authorImage release];
    [_infoView release];
    [_cover release];
    [_comment release];
    [_praise release];
    [_share release];
    [_title release];
    [_partInfo release];
    [_coverShadow release];
    [_commentShadow release];
    [_praiseShadow release];
    [_shareShadow release];
    [_commentCount release];
    [_shareCount release];
    [_praiseCount release];
    [_buttonBg release];
    [super dealloc];
}


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // 初始化
        self.author = [[[UILabel alloc] init] autorelease];
        self.authorImage  = [[[UIImageView alloc] init] autorelease];
        self.infoView = [[[UIView alloc] init] autorelease];
        self.cover = [[[UIImageView alloc] init] autorelease];
        self.comment = [[[UIButton alloc] init] autorelease];
        self.praise = [[[UIButton alloc] init] autorelease];
        self.share = [[[UIButton alloc] init] autorelease];
        self.title = [[[UILabel alloc] init] autorelease];
        self.partInfo = [[[UILabel alloc] init] autorelease];
        self.coverShadow = [[[UIView alloc] init] autorelease];
        self.praiseShadow = [[[UIView alloc] init] autorelease];
        self.shareShadow = [[[UIView alloc] init] autorelease];
        self.commentShadow = [[[UIView alloc] init] autorelease];
        self.more = [[[UIButton alloc] init] autorelease];
        self.coverTitle = [[[UIView alloc] init] autorelease];
        self.commentCount = [[[UILabel alloc] init] autorelease];
        self.praiseCount = [[[UILabel alloc] init] autorelease];
        self.shareCount = [[[UILabel alloc] init] autorelease];
        self.buttonBg = [[[UIView alloc] init] autorelease];
        // 层级关系
        // 阴影
        [self.coverShadow addSubview:self.cover];
        [self.commentShadow addSubview:self.comment];
        [self.praiseShadow addSubview:self.praise];
        [self.shareShadow addSubview:self.share];
        [self.cover addSubview:self.coverTitle];
        [self.infoView addSubview:self.author];
        [self.infoView addSubview:self.authorImage];
        [self.infoView addSubview:self.coverShadow];
        [self.buttonBg addSubview:self.commentShadow];
        [self.buttonBg addSubview:self.shareShadow];
        [self.buttonBg addSubview:self.praiseShadow];
        [self.infoView addSubview:self.more];
        [self.comment addSubview:self.commentCount];
        [self.share addSubview:self.shareCount];
        [self.praise addSubview:self.praiseCount];
        [self.coverTitle addSubview:self.title];
        [self.coverTitle addSubview:self.partInfo];
        [self.infoView addSubview:self.buttonBg];
        [self.contentView addSubview:self.infoView];
        // 背景颜色
        self.contentView.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:1];
        self.infoView.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:1];
        self.buttonBg.backgroundColor = [UIColor lightGrayColor];
        // button的式样
        [self.share setBackgroundColor:[UIColor colorWithRed:1.0 green:0.698 blue:0.2196 alpha:1]];
        [self.praise setBackgroundColor:[UIColor colorWithRed:1.0 green:0.698 blue:0.2196 alpha:1]];
        [self.comment setBackgroundColor:[UIColor colorWithRed:1.0 green:0.698 blue:0.2196 alpha:1]];
        //        [self.praise setBackgroundColor:[UIColor colorWithRed:0.9333 green:0.2471 blue:0.2392 alpha:1]];
        //        [self.comment setBackgroundColor:[UIColor colorWithRed:0.0 green:0.4549 blue:0.9137 alpha:1]];
        //        [self.share.layer setBorderWidth:3];
        //        [self.share.layer setBorderColor:[UIColor colorWithRed:1.0 green:0.698 blue:0.2196 alpha:1].CGColor];
        //        [self.comment.layer setBorderWidth:3];
        //        [self.comment.layer setBorderColor:[UIColor colorWithRed:0.9333 green:0.2471 blue:0.2392 alpha:1].CGColor];
        //        [self.praise.layer setBorderWidth:3];
        //        [self.praise.layer setBorderColor:[UIColor colorWithRed:0.0 green:0.4549 blue:0.9137 alpha:1].CGColor];
        
        self.comment.layer.cornerRadius = 10;
        self.praise.layer.cornerRadius = 10;
        self.share.layer.cornerRadius = 10;
        
        
        [self.praise setImage:[UIImage imageNamed:@"iconfont-iconhuo16pxx16px01-2.png"] forState:UIControlStateNormal];
        [self.comment setImage:[UIImage imageNamed:@"iconfont-comment.png" ] forState:UIControlStateNormal];
        [self.share setImage:[UIImage imageNamed:@"iconfont-share-2.png" ] forState:UIControlStateNormal];
        //        self.praise.imageEdgeInsets = UIEdgeInsetsMake(10, 10, 2, 85);
        //        self.comment.imageEdgeInsets = UIEdgeInsetsMake(11, 10, 2, 88);
        //        self.share.imageEdgeInsets = UIEdgeInsetsMake(11, 10, 3, 85);
        self.praise.imageEdgeInsets = UIEdgeInsetsMake(12, 12, 4, 88.5);
        self.comment.imageEdgeInsets = UIEdgeInsetsMake(13, 12, 4, 90);
        self.share.imageEdgeInsets = UIEdgeInsetsMake(13, 12, 5, 87);
        //self.praise.imageEdgeInsets = UIEdgeInsetsMake(7, 20.5, 18, 12.5);
        //self.praise.imageEdgeInsets = UIEdgeInsetsMake(0, 25.5, 10, 0);
        // button阴影
        //        self.praiseShadow.backgroundColor = [UIColor colorWithRed:0.9333 green:0.4 blue:0.24 alpha:0.6];
        self.praiseShadow.layer.cornerRadius = 10;
        //        self.commentShadow.backgroundColor = [UIColor colorWithRed:0.0 green:0.4549 blue:0.9137 alpha:0.4];
        self.commentShadow.layer.cornerRadius = 10;
        //        self.shareShadow.backgroundColor = [UIColor colorWithRed:1.0 green:0.9 blue:0.7 alpha:1];
        self.shareShadow.layer.cornerRadius = 10;
        self.praiseShadow.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.3];
        self.shareShadow.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.3];
        self.commentShadow.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.2];
        
        //        self.praiseShadow.layer.borderWidth = 1;
        //        self.praiseShadow.layer.borderColor = [UIColor colorWithRed:0.5 green:0.4 blue:1.0 alpha:0.6].CGColor;
        //        self.shareShadow.layer.borderWidth = 1;
        //
        //        self.shareShadow.layer.borderColor = [UIColor colorWithRed:1.0 green:0.9 blue:0.7 alpha:1].CGColor;
        //
        //        self.commentShadow.layer.borderWidth = 1;
        //        self.commentShadow.layer.borderColor = [UIColor colorWithRed:0.9333 green:0.4 blue:0.24 alpha:0.4].CGColor;
        
        [self.more setTitle:@">>专题" forState:UIControlStateNormal];
        self.more.titleLabel.font = [UIFont systemFontOfSize:13];
        [self.more setTitleColor:[UIColor colorWithRed:0.5039 green:0.5039 blue:0.5039 alpha:1] forState:UIControlStateNormal];
        
        // view式样
        [self.infoView bringSubviewToFront:self.coverShadow];
        [self.infoView.layer setBorderWidth:1];
        [self.infoView.layer setBorderColor:[UIColor colorWithRed:1 green:1 blue:1 alpha:1].CGColor];
        self.coverShadow.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.3];
        self.coverTitle.backgroundColor= [UIColor colorWithRed:0 green:0 blue:0 alpha:0.4];
        self.cover.layer.masksToBounds = YES;
        self.cover.layer.cornerRadius = 2;
        self.cover.image = [UIImage imageNamed:@"Fastlook_placeholder.png"];
        self.coverShadow.layer.cornerRadius = 2;
        self.title.textAlignment = NSTextAlignmentLeft;
        self.partInfo.textAlignment = NSTextAlignmentRight;
        self.title.textColor = [UIColor whiteColor];
        self.partInfo.textColor = [UIColor whiteColor];
        self.partInfo.font = [UIFont systemFontOfSize:14];
        self.title.font = [UIFont systemFontOfSize:16];
        self.authorImage.layer.masksToBounds = YES;
        self.authorImage.layer.cornerRadius = 10;
        self.authorImage.layer.borderWidth = 1;
        self.authorImage.layer.borderColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1].CGColor;
        self.author.textColor = [UIColor colorWithRed:0.4 green:0.4 blue:0.4 alpha:1];
        self.author.font = [UIFont systemFontOfSize:14];
        self.commentCount.textAlignment = NSTextAlignmentCenter;
        self.commentCount.textColor = [UIColor whiteColor];
        self.commentCount.font = [UIFont systemFontOfSize:14];
        self.shareCount.textAlignment = NSTextAlignmentCenter;
        self.shareCount.textColor = [UIColor whiteColor];
        self.shareCount.font = [UIFont systemFontOfSize:14];
        self.praiseCount.textAlignment = NSTextAlignmentCenter;
        self.praiseCount.textColor = [UIColor whiteColor];
        self.praiseCount.font = [UIFont systemFontOfSize:14];
        self.buttonBg.backgroundColor = [UIColor colorWithRed:0.8 green:0.8 blue:0.8 alpha:0.5];
//        [self.praise addTarget:self action:@selector(got) forControlEvents:UIControlEventTouchUpInside];
        [self.share addTarget:self action:@selector(goToShare) forControlEvents:UIControlEventTouchUpInside];
        [self.comment addTarget:self action:@selector(goToComment) forControlEvents:UIControlEventTouchUpInside];
        [self.more addTarget:self action:@selector(goToAllContent) forControlEvents:UIControlEventTouchUpInside];
        self.coverShadow.layer.cornerRadius = 4;
        UITapGestureRecognizer *tapCover = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(pushToDetail)];
        [self.cover addGestureRecognizer:tapCover];
        [tapCover release];
//        self.contentView.userInteractionEnabled = YES;
//        self.infoView.userInteractionEnabled = YES;
//        self.coverShadow.userInteractionEnabled = YES;
        self.cover.userInteractionEnabled = YES;
        UITapGestureRecognizer *tapAuthor = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(goToAuthor)];
        [self.authorImage addGestureRecognizer:tapAuthor];
        [tapAuthor release];
        self.authorImage.userInteractionEnabled = YES;
    }
    return self;
}



- (void)layoutSubviews
{
    self.infoView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 95 + SCREEN_WIDTH * 0.48);
    
    //    self.coverShadow.frame = CGRectMake(0, 30, SCREEN_WIDTH * 0.8533 + 1.5, SCREEN_WIDTH * 0.48 + 1.5);
    //    self.cover.frame = CGRectMake(0, 0, SCREEN_WIDTH * 0.8533, SCREEN_WIDTH * 0.48);
    self.coverShadow.frame = CGRectMake(0, 30, SCREEN_WIDTH, SCREEN_WIDTH * 0.5625);
    self.cover.frame = CGRectMake(0.5, 0, SCREEN_WIDTH - 1, self.coverShadow.frame.size.height - 1.5);
    
    self.buttonBg.frame = CGRectMake(0, self.cover.frame.size.height + 21, SCREEN_WIDTH, 44);
    //    self.praise.frame = CGRectMake(0, 0.5, SCREEN_WIDTH * 0.1467 + 5, (SCREEN_WIDTH * 0.48 - 23) * 0.3333  - 1);
    //    self.praiseShadow.frame = CGRectMake(SCREEN_WIDTH * 0.8533 - 10, 31, (SCREEN_WIDTH * 0.1457 - 4) + 10, (SCREEN_WIDTH * 0.48 - 23) * 0.3333 );
    self.praise.frame = CGRectMake(0.5, 1, (SCREEN_WIDTH - 20) / 3 - 1, 35 - 2);
    self.praiseShadow.frame = CGRectMake(5, 0, (SCREEN_WIDTH - 20) / 3, 35);
    
    
    //    self.commentShadow.frame = CGRectMake(SCREEN_WIDTH * 0.8533 - 10, 31 + self.praiseShadow.frame.size.height - 0.5, (SCREEN_WIDTH * 0.1467 - 4) + 10, (SCREEN_WIDTH * 0.48 - 23) * 0.3333 );
    //    self.comment.frame = CGRectMake(0, 0.5, SCREEN_WIDTH * 0.1467 + 5, (SCREEN_WIDTH * 0.48 - 23) * 0.3333  - 1);
    self.commentShadow.frame = CGRectMake(self.praiseShadow.frame.size.width + 10, self.praiseShadow.frame.origin.y, (SCREEN_WIDTH - 20) / 3, 35);
    self.comment.frame = CGRectMake(0.5, 1, self.commentShadow.frame.size.width - 1, self.commentShadow.frame.size.height - 2);
    
    //    self.share.frame = CGRectMake(0, 0.5, SCREEN_WIDTH * 0.1467 + 5, (SCREEN_WIDTH * 0.48 - 23) * 0.3333  - 1);
    //    self.shareShadow.frame = CGRectMake(SCREEN_WIDTH * 0.8533 - 10, self.commentShadow.frame.origin.y + self.commentShadow.frame.size.height - 0.5, (SCREEN_WIDTH * 0.1467 - 4) + 10, (SCREEN_WIDTH * 0.48 - 23) * 0.3333);
    self.shareShadow.frame = CGRectMake(self.commentShadow.frame.origin.x + self.commentShadow.frame.size.width + 5, self.praiseShadow.frame.origin.y, (SCREEN_WIDTH - 20) / 3, 35);
    self.share.frame = CGRectMake(0.5, 1, self.shareShadow.frame.size.width - 1, self.shareShadow.frame.size.height - 2);
    
    self.coverTitle.frame = CGRectMake(0, 1, self.cover.frame.size.width, 30);
    self.title.frame = CGRectMake(0, 0, self.coverTitle.frame.size.width, 30);
    self.partInfo.frame = self.title.frame;
    
    self.authorImage.frame = CGRectMake(5, 5, 20, 20);
    self.author.frame = CGRectMake(30, 5, 200, 20);
    self.more.frame = CGRectMake(SCREEN_WIDTH - 60, 3, 60, 24);
    self.commentCount.frame = CGRectMake(0, 0, 50, 12);
    self.shareCount.frame = CGRectMake(0, 0, 50, 12);
    self.praiseCount.frame = CGRectMake(0, 0, 50, 12);
    self.commentCount.center = CGPointMake(self.comment.center.x + 5, self.comment.frame.size.height - 12);
    self.shareCount.center = CGPointMake(self.share.center.x + 5, self.share.frame.size.height - 12);
    self.praiseCount.center = CGPointMake(self.praise.center.x + 5, self.praise.frame.size.height - 12);
    
    
    
}



- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)setModel:(ZYLFastLookModel *)model
{
    if (_model != model) {
        [_model release];
        _model = [model retain];
    }
    self.partInfo.text = model.title;
    self.title.text = [model.topic objectForKey:@"title"];
    [self.cover sd_setImageWithURL:[NSURL URLWithString:model.cover_image_url] placeholderImage:[UIImage imageNamed:@"Fastlook_placeholder.png"]];
    self.author.text = [[model.topic objectForKey:@"user"] objectForKey:@"nickname"];
    [self.authorImage sd_setImageWithURL:[NSURL URLWithString:[[model.topic objectForKey:@"user"] objectForKey:@"avatar_url"]] placeholderImage:[UIImage imageNamed:@"iconfont-user.png"]];
    self.praiseCount.text = [NSString stringWithFormat:@"%ld", model.likes_count];
    self.shareCount.text = [NSString stringWithFormat:@"%ld", model.shared_count];
    self.commentCount.text = [NSString stringWithFormat:@"%ld", model.comments_count];
    
}


/*************************** 一些跳转方法 *************************/

// 点击图片
- (void)pushToDetail
{
#warning 跳转到阅读页面
    [self.pushToOtherDelegate pushToOtherViewControllerWithZYLFastLookModel:self.model keyword:@"图片"];
    NSLog(@"点击图片");
}

// 点击专题
- (void)goToAllContent
{
    [self.pushToOtherDelegate pushToOtherViewControllerWithZYLFastLookModel:self.model keyword:@"专题"];
    NSLog(@"点击专题");
}

// 打开评论
- (void)goToComment
{
    [self.pushToOtherDelegate pushToOtherViewControllerWithZYLFastLookModel:self.model keyword:@"评论"];
    NSLog(@"点击评论");
}

// 打开分享
- (void)goToShare
{
    [self.pushToOtherDelegate pushToOtherViewControllerWithZYLFastLookModel:self.model keyword:@"分享"];
    NSLog(@"点击分享");
}

// 打开作者页
- (void)goToAuthor
{
    [self.pushToOtherDelegate pushToOtherViewControllerWithZYLFastLookModel:self.model keyword:@"作者"];
    NSLog(@"点击作者头像");
}


@end
